﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using Common;
using MySql.Data.MySqlClient;
using GameServer.Tool;
using GameServer.Servers;
using GameServer.Model;

namespace GameServer.Servers
{
    class Client
    {
        //一个客户端
        private Socket clientSocket;
        //服务器
        private Server server;
        //用来解析消息
        private Message msg = new Message();
        private MySqlConnection mysqlConn;

        private User user;
        private Result result;

        public MySqlConnection MySQLConn
        {
            get { return mysqlConn; }
        }

        /// <summary>
        /// 设置玩家数据
        /// </summary>
        /// <param name="_user"></param>
        /// <param name="_result"></param>
        public void SetUserData(User _user, Result _result)
        {
            this.user = _user;
            this.result = _result;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public string GetUserData()
        {
            return user.Username + "," + result.Total + "," + result.Win;
        }

        public Client() { }
        public Client(Socket _clientSocket, Server _server)
        {
            this.clientSocket = _clientSocket;
            this.server = _server;
            mysqlConn = ConnHelper.Connect();
        }

        public void Start()
        {
            if (clientSocket == null || clientSocket.Connected == false)
                return;
            clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.ReaminSize, SocketFlags.None, ReceiveCallBack, null);
        }

        /// <summary>
        /// 接收回调
        /// </summary>
        /// <param name="ar"></param>
        private void ReceiveCallBack(IAsyncResult ar)
        {
            try
            {
                if (clientSocket == null || clientSocket.Connected == false)
                    return;
                int count = clientSocket.EndReceive(ar);
                if (count == 0)
                {
                    Close();
                }
                //TODO 处理接收到的数据
                msg.ReadMessage(count, OnProcessMessage);
                //clientSocket.BeginReceive(msg.Data, msg.StartIndex, msg.ReaminSize, SocketFlags.None, ReceiveCallBack, null);
                Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Close();
            }
        }

        private void OnProcessMessage(RequestCode requestCode, ActionCode actionCode, string data)
        {
            server.HandleRequest(requestCode, actionCode, data, this);
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        private void Close()
        {
            ConnHelper.CloseConnection(mysqlConn);
            if (clientSocket != null)
                clientSocket.Close();
            server.RemoveClient(this);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="requeseCode"></param>
        /// <param name="data"></param>
        public void Send(ActionCode actionCode, string data)
        {
            byte[] bytes = Message.PackData(actionCode, data);
            clientSocket.Send(bytes);
        }
    }
}
